In [1]:
import numpy as np
import pandas as pd
import folium
EXERCISE 1
Create a map of Tenerife.
- Use a zoom start of: 5, 10
- Use 3 different styles as tiles including the following 2: Stamen Terrain, Stamen Watercolor
In [18]:
tenerife_map = folium.Map(
location=[28.217, -16.589],
zoom_start=5,
tiles='https://tiles.stadiamaps.com/tiles/stamen_terrain/{z}/{x}/{y}{r}.png',
attr="Map tiles by Stamen Design, CC BY 3.0 — Map data © OpenStreetMap contributors"
)
tenerife_map
Out[18]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [17]:
tenerife_map = folium.Map(
location=[28.217, -16.589],
zoom_start=10,
tiles="https://tiles.stadiamaps.com/tiles/stamen_watercolor/{z}/{x}/{y}{r}.png",
attr="Map tiles by Stamen Design, CC BY 3.0 — Map data © OpenStreetMap contributors"
)
tenerife_map
Out[17]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [20]:
tenerife_map = folium.Map(
location=[28.217, -16.589],
zoom_start=10,
tiles="OpenStreetMap",
)
tenerife_map
Out[20]:
Make this Notebook Trusted to load map: File -> Trust Notebook
EXERCISE 2
Create a map of Romania with the following properties:
- Use a zoom start of 8.
- Add a feature group and a marker for the following cities: Cluj-Napoca, Sibiu and Timisoara.
- The colours of these markers should be green, pink, orange.
- The icons for the three locations should be: car, flag, plug (selected from here: https://fontawesome.com/icons/categories/humanitarian)
- Add a black line from Cluj-Napoca to Sibiu and from Sibiu to Timisoara (see folium documentation: https://python-visualization.github.io/folium/quickstart.html#Polylines)
In [48]:
ro_map = folium.Map(
location = [45.9432, 24.9668],
zoom_start = 8
)
car_icon = folium.CustomIcon(
icon_image='car-solid.svg',
icon_size=(30,30)
)
flag_icon = folium.CustomIcon(
icon_image='https://fontawesome.com/icons/flag?f=classic&s=solid',
icon_size=(30,30)
)
plug_icon = folium.CustomIcon(
icon_image='https://fontawesome.com/icons/plug?f=classic&s=solid',
icon_size=(30,30)
)
cluj = folium.map.FeatureGroup()
cluj.add_child(
folium.features.CircleMarker(
[46.7784, 23.6172], radius = 5,
color = 'green', fill_color = 'green'
)
)
sibiu = folium.map.FeatureGroup()
sibiu.add_child(
folium.features.CircleMarker(
[45.7973912, 24.1519202], radius = 5,
color = 'pink', fill_color = 'pink'
)
)
timisoara = folium.map.FeatureGroup()
timisoara.add_child(
folium.features.CircleMarker(
[45.7538355, 21.2257474], radius = 5,
color = 'orange', fill_color = 'orange'
)
)
ro_map.add_child(cluj)
ro_map.add_child(sibiu)
ro_map.add_child(timisoara)
folium.Marker(
location=[46.7784, 23.6172],
popup='Cluj-Napoca',
icon=folium.Icon(color='green', icon='car', prefix='fa')
).add_to(ro_map)
folium.Marker(
location=[45.7973912, 24.1519202],
popup='Sibiu',
icon=folium.Icon(color='pink', icon='flag', prefix='fa')
).add_to(ro_map)
folium.Marker(
location=[45.7538355, 21.2257474],
popup='Timisoara',
icon=folium.Icon(color='orange', icon='plug', prefix='fa')
).add_to(ro_map)
folium.PolyLine(
locations=[[46.7784, 23.6172],[45.7973912, 24.1519202]],
color='#000000'
).add_to(ro_map)
folium.PolyLine(
locations=[[45.7973912, 24.1519202], [45.7538355, 21.2257474]],
color='#000000'
).add_to(ro_map)
ro_map
Out[48]:
Make this Notebook Trusted to load map: File -> Trust Notebook
EXERCISE 3
Create a Choropleth map of the world showing the immigration to Canada for the last 3 years available in the Canada Immigration Dataset (i.e. years 2011, 2012 and 2013).
- Set a specific zoom start.
- Choose a new color pallette.
- Set any other parameters you see fit to make the map look nice.
In [50]:
df_canada = pd.read_excel("../datasets/Canada.xlsx",
sheet_name="Canada by Citizenship",
skiprows=range(20),
skipfooter=2)
# 1. Remove columns that are not necessary
df_canada.drop(['AREA', 'REG', 'DEV', 'Type', 'Coverage'], axis=1, inplace=True)
# 2. Rename some columns
df_canada.rename(columns={'OdName':'Country', 'AreaName':'Continent','RegName':'Region'}, inplace=True)
# 3. Column labels should be strings
df_canada.columns = list(map(str, df_canada.columns))
# 4. Set the index to the country column
# df_canada.set_index('Country', inplace=True)
# 5. Add an extra column: Total
df_canada['Total'] = df_canada.sum(axis=1, numeric_only = True)
# Create a list of years from 1980 - 2013 as strings
years = list(map(str, range(1980, 2014)))
world_geo = r'world_countries.json' # geojson file
# create a plain world map
world_map = folium.Map(location=[0, 0], zoom_start=3)
folium.Choropleth(
geo_data=world_geo,
data=df_canada,
columns=['Country', '2011', '2012', '2013'], # Must pass column 1 as the key, and column 2 the values.
key_on='feature.properties.name',
fill_color='Greens',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Immigration to Canada'
).add_to(world_map)
world_map
Out[50]:
Make this Notebook Trusted to load map: File -> Trust Notebook
EXERCISE 4
Create a Choropleth map of Romania using any data you find for each county.
For example:
- Voting data: https://prezenta.bec.ro/europarlamentare26052019/romania-pv-final
- Salar mediu: https://insse.ro/cms/sites/default/files/com_presa/com_pdf/cs09r22.pdf
Geo-spatial data for Romania:
In [67]:
romania_geo = r'ro_judete_poligon.json'
romania_map = folium.Map(location=[45.9432, 24.9668], zoom_start=6)
# folium.Choropleth
df = pd.read_csv('prezenta_vot_prezidential.csv')
df_prezenta = df[['Judet', 'Înscriși pe liste permanente', 'LT']]
sums = df.groupby('Judet')[['Înscriși pe liste permanente', 'LT']].sum().reset_index()
sums['Percent_LT'] = (sums['LT'] / sums['Înscriși pe liste permanente']) * 100
# print(sums)
folium.Choropleth(
geo_data=romania_geo,
data=sums,
columns=['Judet', 'Percent_LT'], # Must pass column 1 as the key, and column 2 the values.
key_on='feature.properties.name',
fill_color='Purples',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Prezenta vot europarlamentare'
).add_to(romania_map)
romania_map
Out[67]:
Make this Notebook Trusted to load map: File -> Trust Notebook